package com.webserver.controller;

import com.webserver.annotation.Controller;
import com.webserver.annotation.RequestMapping;
import com.webserver.core.DispatcherServlet;
import com.webserver.entity.User;
import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;

import java.io.*;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

/**
 * 处理与用户相关的业务操作
 */
@Controller
public class UserController {
    private static File userDir;

    static{
        userDir = new File("./users");
        if(!userDir.exists()){
            userDir.mkdirs();
        }
    }

    @RequestMapping("/myweb/reg")
    public void reg(HttpServletRequest request, HttpServletResponse response){
        //1 获取用户注册页面上输入的注册信息，获取form表单提交的内容
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String nickname = request.getParameter("nickname");
        String ageStr = request.getParameter("age");

        if(username==null || password==null || nickname==null || ageStr==null || !ageStr.matches("[0-9]+")){
//            File file = new File(staticDir,"/myweb/reg_input_error.html");
//            response.setContentFile(file);
            response.sendRedirect("/myweb/reg_input_error.html");
            return;
        }
        /*
            必要的验证，要求:
            四项信息不能为null，并且年龄必须是一个数字(正则表达式)
            否则直接给用户一个注册失败的页面:reg_input_error.html
            该页面居中显示一行字:输入信息有误，注册失败
            实现思路:
            添加一个分支判断，如果符合了上述的情况，直接创建一个File对象表示
            错误提示页面，然后将其设置到响应对象的正文上即可，否则才执行下面
            原有的注册条件
         */
        int age = Integer.parseInt(ageStr);
        System.out.println(username+","+password+","+nickname+","+ageStr);

        //2 将用户信息保存
        File userFile = new File(userDir,username+".obj");
        if(userFile.exists()){
//            File file = new File(staticDir,"myweb/have_user.html");
//            response.setContentFile(file);
            response.sendRedirect("/myweb/have_user.html");
            return;
        }
        try (
                FileOutputStream fos = new FileOutputStream(userFile);
                ObjectOutputStream oos = new ObjectOutputStream(fos);
        ){
            User user = new User(username,password,nickname,age);
            oos.writeObject(user);

            //注册成功了
//            File file = new File(staticDir,"/myweb/reg_success.html");
//            response.setContentFile(file);
            response.sendRedirect("/myweb/reg_success.html");

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        //3 给用户响应一个注册结果页面(注册成功或注册失败)
    }

    @RequestMapping("/myweb/login")
    public void login(HttpServletRequest request,HttpServletResponse response){
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        if(username==null || password==null){
//            File file = new File(staticDir,"myweb/login_info_error.html");
//            response.setContentFile(file);
            response.sendRedirect("/myweb/login_info_error.html");
            return;
        }

        File userFile = new File(userDir,username+".obj");
        if(userFile.exists()){
            try (
                 FileInputStream fis = new FileInputStream(userFile);
                 ObjectInputStream ois = new ObjectInputStream(fis);
            ){
                User user = (User)ois.readObject();
                if(user.getPassword().equals(password)){
//                    File file = new File(staticDir,"myweb/login_success.html");
//                    response.setContentFile(file);
                    response.sendRedirect("/myweb/login_success.html");
                    return;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

//        File file = new File(staticDir,"myweb/login_fail.html");
//        response.setContentFile(file);
        response.sendRedirect("/myweb/login_fail.html");

    }

    @RequestMapping("/myweb/showAllUser")
    public void showAllUser(HttpServletRequest request,HttpServletResponse response){
        //1 先用users目录里将所有的用户读取出来，存入一个List集合备用
        List<User> userList = new ArrayList<>();
        File[] users = userDir.listFiles(f->f.getName().endsWith(".obj"));
        for (File userFile : users) {
            try (
                    FileInputStream fis = new FileInputStream(userFile);
                    ObjectInputStream ois = new ObjectInputStream(fis);
            ) {
                //User user = (User) ois.readObject();
                userList.add((User)ois.readObject());
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        //2 使用程序生成一个页面，同时遍历List集合将用户信息拼接到表格中
        PrintWriter pw = response.getWriter();
        pw.println("<!DOCTYPE html>");
        pw.println("<html lang=\"en\">");
        pw.println("<head>");
        pw.println("<meta charset=\"UTF-8\">");
        pw.println("<title>用户列表</title>");
        pw.println("</head>");
        pw.println("<body>");
        pw.println("<center>");
        pw.println("<h1>用户列表</h1>");
        pw.println("<table border=\"1\">");
        pw.println("<tr>");
        pw.println("<td>用户名</td>");
        pw.println("<td>密码</td>");
        pw.println("<td>昵称</td>");
        pw.println("<td>年龄</td>");
        pw.println("<td>操作</td>");
        pw.println("</tr>");
        for(User user : userList){
            pw.println("<tr>");
            pw.println("<td>"+user.getUsername()+"</td>");
            pw.println("<td>"+user.getPassword()+"</td>");
            pw.println("<td>"+user.getNickname()+"</td>");
            pw.println("<td>"+user.getAge()+"</td>");
            pw.println("<td><a href='/myweb/deleteUser?username="+user.getUsername()+"'>删除</a></td>");
            pw.println("</tr>");
        }
        pw.println("</table>");
        pw.println("</center>");
        pw.println("</body>");
        pw.println("</html>");

        //3 设置响应头Content-Type用于告知动态数据是什么
        response.setContentType("text/html");
    }

    @RequestMapping("/myweb/deleteUser")
    public void deleteUser(HttpServletRequest request,HttpServletResponse response){
        String username = request.getParameter("username");

        File userFile = new File(userDir,username+".obj");
        userFile.delete();

        response.sendRedirect("/myweb/showAllUser");

    }

}
